package com.chen.servlet;

import com.chen.pojo.User;
import com.chen.service.user.UserServiceImp;
import com.chen.util.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("LoginServlet---start---");
        //用户前端输入的用户名和密码
        String userCode=req.getParameter("userCode");
        String password = req.getParameter("password");
        //调用业务层
        UserServiceImp userServiceImp = new UserServiceImp();
        User user = userServiceImp.login(userCode, password);
        if (user!=null){
            //查有此人，将用户信息放到session中
            req.getSession().setAttribute(Constants.USER_SESSION,user);
            //跳转到主页
            resp.sendRedirect("jsp/frame.jsp");
        }else {
            //查无此人，转发回登录页面，并提示用户名或密码错误
            req.setAttribute("error","用户名或密码错误");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
